import { createRouter, createWebHistory, createWebHashHistory } from "vue-router";
import { defineAsyncComponent } from "vue";

const router = createRouter({
  // history: createWebHashHistory(),  // hash 模式
  history: createWebHistory(), // history 模式
  routes: [
    {
      path: "/",
      name: "home",
      component: defineAsyncComponent(() => import(`../views/home/index.vue`)),
      meta: {
        title: "首页",
      },
    },
    {
      path: "/formView",
      name: "formView",
      component: defineAsyncComponent(() => import(`../views/formView/index.vue`)),
      meta: {
        title: "表单查看",
      },
    },
    {
      path: "/chat",
      name: "chat",
      component: defineAsyncComponent(() => import(`../views/aiChat/index.vue`)),
      meta: {
        title: "ai聊天",
      },
    },
    {
      path: "/formCreate",
      name: "formCreate",
      component: defineAsyncComponent(() => import(`../views/formCreate/index.vue`)),
      meta: {
        title: "表单设计",
      },
    },
    {
      path:"/login",
      name: "login",
      component: defineAsyncComponent(() => import(`../views/login/login.vue`)),
      meta: {
        title: "登录",
      },
    },
    {
      path:"/first",
      name: "first",
      component: defineAsyncComponent(() => import(`../views/login/firstLogin.vue`)),
      meta: {
        title: "首次登录",
      },
    },
    {
      path:"/register",
      name: "register",
      component: defineAsyncComponent(() => import(`../views/login/register.vue`)),
      meta: {
        title: "注册",
      },
    },
    {
      path: "/*",
      redirect: "/",
    },
  ],
});

// 全局路由守卫
router.beforeEach((to, from, next) => {
  // console.log(to, from)
  if (to.meta.title) {
    document.title = `${to.meta.title}`;
  }
  next();
});

router.afterEach((to, from) => {
  // console.log(to, from)
  // console.log("afterEach");
});

export default router;
